<?php 
ob_start();
if(!isset($_SESSION)){
session_start(); 
}
	require_once('../../Connections/microassur.php');
	require_once('../../utils/fonctions.php');
	$message = '';
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if( (isset($_POST['Nom_personne']) && empty($_POST['Nom_personne'])) && (isset($_POST['Raison_sociale']) && empty($_POST['Raison_sociale'])) ){
	$message = colorer("Vous devez saisir au moins le NOM ou la RAISON SOCIALE de ce partenaire.","false");
}else{
	if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1") and $_POST['Type_personne']=="P") {
	
	
	//Vérification condition ventes 
	
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1") &&  isset($_POST['Type_personne'])&&  $_POST['Type_personne'] =='P')
 $bool_date_naiss= datevalide($_POST['Date_naissance_personne']);
elseif(isset($_POST['Type_personne'])&& $_POST['Type_personne']=='M')$bool_date_naiss=false;



$offre=-1;
$formule=-1;
$produit=-1;


//RETROUVER l'offre, le produit et la formule de l'intervenant
//Formule
$requete1=mysql_query("select * from garantiesouscrite where Identifiant_personne='".$_GET['Identifiant_personne']."'") or die(mysql_error());
$reponse1=mysql_fetch_array($requete1);
$formule=$reponse1['Identifiant_formule']; 
	
//Produit
$requete2=mysql_query("select * from garantie,garantiesouscrite where garantie.Identifiant_garantie=garantiesouscrite.Identifiant_garantie and garantiesouscrite.Identifiant_personne='".$_GET['Identifiant_personne']."'") or die(mysql_error());
$reponse2=mysql_fetch_array($requete2);
$produit=$reponse2['Identifiant_produit']; 


//offre

$requete3=mysql_query("select * from produit where Identifiant_produit='".$produit."'") or die(mysql_error());
$reponse3=mysql_fetch_array($requete3);
$offre=$reponse3['Identifiant_offre']; 


$QuestionBase = "SELECT * from conditionvente  WHERE (Identifiant_offre='".$offre."' or Identifiant_produit='".$produit."' ) and (Code_condition_vente='AGMIA' or Code_condition_vente='AGMIE' or Code_condition_vente='AGMAA' or Code_condition_vente='AGMAI' )  and Type_condition_vente='L' ORDER BY Identifiant_condition_vente ASC " ;
$result_recherche=mysql_query($QuestionBase, $microassur) or die (mysql_error());
$rows = mysql_num_rows($result_recherche);
$bool1=0; 
	if( $rows || (isset($_POST['Type_personne'])&&  $_POST['Type_personne'] =='P'))
	 { 
		 
			 if( (isset($_POST['Type_personne'])&&  $_POST['Type_personne'] =='P'))
		{
				 $date_naiss=$_POST['Date_naissance_personne'];
				 $date_naiss1=explode("-",$date_naiss);
				 $age=date('Y')-$date_naiss1[2]; 
					
					 $QuestionBase = "SELECT * from conditionvente  WHERE (Identifiant_offre='".$offre."' or Identifiant_produit='".$produit."') and (Code_condition_vente='AGMIE' or Code_condition_vente='AGMIA')  ORDER BY Identifiant_condition_vente ASC " ;
			$result_recherche=mysql_query($QuestionBase, $microassur) or die (mysql_error());
			if($result_recherche )
				{
					if($bool_date_naiss )
					{
					$result_recherche_fetch=mysql_fetch_array($result_recherche);
						 if( $age<$result_recherche_fetch['Valeur_condition_vente']){$bool1++;$message=$message.colorer("La personne est plus jeune que l’âge requis à l’adhésion","false");}
						 
							 else
							 { 
							 $QuestionBase = "SELECT * from conditionvente  WHERE (Identifiant_offre='".$offre."' or Identifiant_produit='".$produit."') and (Code_condition_vente='AGMAE'  or Code_condition_vente='AGMAA') ORDER BY Identifiant_condition_vente ASC " ;
						$result_recherche=mysql_query($QuestionBase, $microassur) or die (mysql_error());
						$result_recherche_fetch=mysql_fetch_array($result_recherche);
						
							 if( $result_recherche && $age>$result_recherche_fetch['Valeur_condition_vente']){$bool1++;$message=$message.colorer("La personne a dépassé l’âge limite à l’adhésion","false");}
							 }
					}
				} //elseif(!$bool_date_naiss)$messge=colorer("date invalide","false");
		}
		
}
	
	
	if($bool1==0){
	
	  // Mise à jour d'une Personne physique
	  if (!(strcmp("P", htmlentities(typePersonne($database_microassur, $microassur, $_GET['Identifiant_personne']), ENT_COMPAT, 'utf-8')))){
		  $updateSQL = sprintf("UPDATE personne SET Type_personne=%s, Etat_personne=%s, Nom_personne=%s, Prenom_personne=%s, Civilite_personne=%s, Nom_jeune_fille_personne=%s, Date_naissance_personne=%s, Lieu_naissance_personne=%s, Sexe_personne=%s, Profession_personne=%s, Employeur_personne=%s WHERE Identifiant_personne=%s",
							   GetSQLValueString($_POST['Type_personne'], "text"),
							   GetSQLValueString($_POST['Etat_personne'], "text"),
							   GetSQLValueString($_POST['Nom_personne'], "text"),
							   GetSQLValueString($_POST['Prenom_personne'], "text"),
							   GetSQLValueString($_POST['Civilite_personne'], "text"),
							   GetSQLValueString($_POST['Nom_jeune_fille_personne'], "text"),
							   GetSQLValueString(dateEN2FR($_POST['Date_naissance_personne']), "date"),
							   GetSQLValueString($_POST['Lieu_naissance_personne'], "text"),
							   GetSQLValueString($_POST['Sexe_personne'], "text"),
							   GetSQLValueString($_POST['Profession_personne'], "text"),
							   GetSQLValueString($_POST['Employeur_personne'], "text"),
							   GetSQLValueString($_POST['Identifiant_personne'], "int"));
			}
			else{ // Mise à jour d'une Personne morale
		  $updateSQL = sprintf("UPDATE personne SET Type_personne=%s, Etat_personne=%s, Raison_sociale=%s WHERE Identifiant_personne=%s",
							   GetSQLValueString($_POST['Type_personne'], "text"),
							   GetSQLValueString($_POST['Etat_personne'], "text"),
							   GetSQLValueString($_POST['Raison_sociale'], "text"),
							   GetSQLValueString($_POST['Identifiant_personne'], "int"));				
			}
	
	  mysql_select_db($database_microassur, $microassur);
	  $Result1 = mysql_query($updateSQL, $microassur) or die(mysql_error());
	  if($Result1){
		$message =$message. colorer("La modification de la personne est prise en compte","false");
	  }else{
		$message =$message. colorer("Problème modification partenaire, contacter la DSI","false");
	  }
	  }
	}
}
$colname_rs_mod = "-1";
if (isset($_GET['Identifiant_personne'])) {
  $colname_rs_mod = $_GET['Identifiant_personne'];
  if (isset($_GET['id']))
    $_SESSION['']=retourValForm('id');
}
mysql_select_db($database_microassur, $microassur);
$query_rs_mod = sprintf("SELECT * FROM personne WHERE Identifiant_personne = %s", GetSQLValueString($colname_rs_mod, "int"));
$rs_mod = mysql_query($query_rs_mod, $microassur) or die(mysql_error());
$row_rs_mod = mysql_fetch_assoc($rs_mod);
$totalRows_rs_mod = mysql_num_rows($rs_mod);

if(isset($_GET['Identifiant_personne'])){
	 $idContrat = IdContrat($database_microassur, $microassur, $_SESSION['num_contrat']);
	 $idPersonne = $_GET['Identifiant_personne'];
	 // Récupérer Type_assure du Role
	 $typeAssure = typeAssureRole($database_microassur, $microassur, $idPersonne, $idContrat);
	 //var_dump($typeAssure);
	}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Modification administrative des éléments du contrat</title>
<link rel="stylesheet" type="text/css" href="../../css/main.css" />
<link rel="stylesheet" type="text/css" href="slist.css" />
			<!--[if IE 7]>
            	<link rel="stylesheet" type="text/css" href="../../css/ie7.css" />
            	<link rel="stylesheet" type="text/css" href="sform_ie7.css" />
            <![endif]-->
<script language="javascript" type="text/javascript">
	  function cacheChamp(x)
	  { 
	  	if(x=="M")
		{
	  	document.getElementById("cache_nom").style.display="none";
		document.getElementById("cache_prenom").style.display="none";
	  	document.getElementById("cache_civ").style.display="none";
	  	document.getElementById("cache_sexe").style.display="none";
	  	document.getElementById("cache_jeunefille").style.display="none";
	  	document.getElementById("cache_date").style.display="none";
	  	document.getElementById("cache_emp").style.display="none";
		document.getElementById("cache_lieu").style.display="none";
	  	document.getElementById("cache_prof").style.display="none";	  	

		}
		else
		{
	  	document.getElementById("cache_nom").style.display="block";
		document.getElementById("cache_prenom").style.display="block";
	  	document.getElementById("cache_civ").style.display="block";
	  	document.getElementById("cache_sexe").style.display="block";
	  	document.getElementById("cache_jeunefille").style.display="block";
	  	document.getElementById("cache_date").style.display="block";
	  	document.getElementById("cache_emp").style.display="block";
		document.getElementById("cache_lieu").style.display="block";
	  	document.getElementById("cache_prof").style.display="block";	  	

		}
	  }
</script>
</head>

<body onLoad="document.forms['form1'].elements['Type_personne'].focus()">
<div id="sf_menu1">
<h3>Production </h3>
<?php
	require('../index.php');
	if(isset($message) && !empty($message)){
		echo $message;
	}
	$nommodule="Personne";
	//$nommodule2="Liste des adresse";
	$nom_module_saisie="Ecran saisie";
?>
</div>
<div class="cleared"></div>

<div class="corps1">
<!--/***************************************/-->
	<div class="imgleft1"></div>
<form action="<?php echo $editFormAction; ?>" method="post"   name="form1" id="form1">
	<div class="imgmiddle1">
    <div class="btn">
    	<span class="btnleft"></span>
        <span class="btnmiddle">
        	<h5><?php echo $nommodule; ?></h5>
        </span><span class="btnright"></span>
    </div>
  	<div class="btn1">
    	<span class="btnleft"></span>
        <span class="btnmiddle">
        	<h5><?php echo $nom_module_saisie; ?></h5>
        </span>
        <span class="btnright"></span>
    </div>
	<div class="formulaire form">
	  <div class="tabform">
       <div  class="ligne" >
      <div class="titre"> TYPE PERSONNE</div>
      <div class="champ">
     
      <select name="Type_personne"   >
      
	 <?php if(htmlentities($row_rs_mod['Type_personne'], ENT_COMPAT, 'utf-8')=="M"){?>
        <option  value="M">Morale</option>
		<?php } else {?>
		<option  value="P">Physique</option>
        <?php }?>
        </select>   
      
	  </div>
	  </div>
		<div  class="ligne">
         <div class="titre"> TYPE  ASSURE</div>
         <div class="champ">
         <select name="Type_assure"   >
      <?php $requete=mysql_query("select elementdico.* from dictionnaire,elementdico where dictionnaire.Code_table='M0003' and dictionnaire.Identifiant_table=elementdico.Identifiant_table");
	 
	  while($reponse=mysql_fetch_array($requete)) {?>
        <option <?php  if(isset($_SESSION['Type_assure'])) echo remind_select1('Type_assure', $reponse["Code_element"]) ; else echo remind_select('Type_assure',$reponse["Code_element"]);?> value="<?php echo $reponse["Code_element"];?>"><?php echo $reponse["Nom_element"];?></option>
        <?php }?>
        </select>      
         </div>
			
		</div>
  
	<div  class="ligne">
		<div class="titre">RAISON SOCIALE</div>
		<div class="champ"><input type="text" name="Raison_sociale" value="<?php echo htmlentities($row_rs_mod['Raison_sociale'], ENT_COMPAT, 'utf-8'); ?>" size="32" maxlength="30" /></div>
    </div>    
  <?php if(htmlentities($row_rs_mod['Type_personne'], ENT_COMPAT, 'utf-8')=="P"){?>
    <div  class="ligne" id="cache_nom">
      <div class="titre"> NOM </div>
      <div class="champ"><input type="text" name="Nom_personne" value="<?php echo htmlentities($row_rs_mod['Nom_personne'], ENT_COMPAT, 'utf-8'); ?>" size="32" maxlength="30" />
    </div></div>
    <div  class="ligne" id="cache_prenom">
      <div class="titre">PRENOM</div>
	  <div class="champ"><input type="text" name="Prenom_personne" value="<?php echo htmlentities($row_rs_mod['Prenom_personne'], ENT_COMPAT, 'utf-8'); ?>" size="32" maxlength="30" />
    </div></div>
    <div class="ligne" id="cache_civ">
      <div class="titre">CIVILITE</div>
      <div class="champ"><input type="text" name="Civilite_personne" value="<?php echo htmlentities($row_rs_mod['Civilite_personne'], ENT_COMPAT, 'utf-8'); ?>" size="32" maxlength="5" />
    </div></div>
    <div   class="ligne" id="cache_jeunefille">
      <div class="titre">NOM JEUNE FILLE</div>
      <div class="champ"><input type="text" name="Nom_jeune_fille_personne" value="<?php echo htmlentities($row_rs_mod['Nom_jeune_fille_personne'], ENT_COMPAT, 'utf-8'); ?>" size="32" maxlength="30" />
    </div></div>
	<div class="ligne" id="cache_date">
	  <div class="titre">DATE DE NAISSANCE</div>
      <div class="champ"><input type="text" name="Date_naissance_personne" value="<?php echo htmlentities(dateEN2FR($row_rs_mod['Date_naissance_personne']), ENT_COMPAT, 'utf-8'); ?>" size="32" />
    </div></div>
    <div class="ligne" id="cache_lieu">
		<div class="titre">LIEU DE NAISSANCE</div>
		<div class="champ"><input type="text" name="Lieu_naissance_personne" value="<?php echo htmlentities($row_rs_mod['Lieu_naissance_personne'], ENT_COMPAT, 'utf-8'); ?>" size="32" maxlength="30" />
    </div></div>
    <div class="ligne" id="cache_sexe">
    <div class="titre">SEXE</div>
      <div class="champ"><select name="Sexe_personne">
        <option value="F" <?php if (!(strcmp("F", htmlentities($row_rs_mod['Sexe_personne'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>>Féminin</option>
        <option value="M" <?php if (!(strcmp("M", htmlentities($row_rs_mod['Sexe_personne'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>>Masculin</option>
      </select>
      </div></div>
	  <div class="cleared"></div>
    <div  class="ligne" id="cache_prof">
      <div class="titre">PROFESSION</div>
      <div class="champ"><input type="text" name="Profession_personne" value="<?php echo htmlentities($row_rs_mod['Profession_personne'], ENT_COMPAT, 'utf-8'); ?>" size="32" maxlength="30" />
    </div></div>
    <div class="ligne" id="cache_emp">
      <div class="titre">EMLOYEUR  </div>
      <div class="champ"><input type="text" name="Employeur_personne" value="<?php echo htmlentities($row_rs_mod['Employeur_personne'], ENT_COMPAT, 'utf-8'); ?>" size="32" maxlength="30" />
    </div></div>
	<?php }  ?>
  
		</div>
		</div>
	
        <?php $id = getIdRole($database_microassur, $microassur, $row_rs_mod['Identifiant_personne']); ?>
	<div class='bt1 inp'> <a href="refexterne/index.php?Identifiant_personne=<?php echo $row_rs_mod['Identifiant_personne']; ?>"><span class='btleft'></span>
        <span class='btmiddle'>Références externes</span>
        <span class='btright'></span>
    </a></div>	
<div class='bt inp'> <a href="communication/index.php?Identifiant_personne=<?php echo $row_rs_mod['Identifiant_personne']; ?>"><span class='btleft'></span>
        <span class='btmiddle'>Communication</span>
        <span class='btright'></span>
    </a></div>	
<div class='bt inp'> <a href="adresse/index.php?Identifiant_personne=<?php echo $row_rs_mod['Identifiant_personne']; ?>"><span class='btleft'></span>
        <span class='btmiddle'>Adresse</span>
        <span class='btright'></span>
    </a></div>
<div class="bt inp"> <a href="index.php?Numero_contrat=<?php echo $_SESSION['num_contrat']; ?>"><span class="btleft"></span>
        <span class="btmiddle">Annuler</span>
        <span class="btright"></span>
    </a></div> 
		<div class="bt inp">
			<a href="index.php?retour=1"><span class="btleft"></span>
				<span class="btmiddle">Retour</span>
				<span class="btright"></span>
			</a>
		</div>
		<div class="bt inp">
				<span class="btleft"></span>
					<input type="submit" value="Valider" <?php if(retourValForm('id')==1) echo "disabled='disabled'"; ?> class="resizeall btmiddle"/>
				<span class="btright"></span>
		</div>

</div>
	 <div class="imgright1"></div>				
	<div class="cleared"></div>
    <!--input type="hidden" name="Type_personne" value="<?php //echo htmlentities($row_rs_mod['Type_personne'], ENT_COMPAT, 'utf-8'); ?>" /-->
	<input type="hidden" name="Etat_personne" value="<?php echo htmlentities($row_rs_mod['Etat_personne'], ENT_COMPAT, 'utf-8'); ?>" />
  <input type="hidden" name="Identifiant_personne" value="<?php echo $row_rs_mod['Identifiant_personne']; ?>" />
  <input type="hidden" name="MM_update" value="form1" />
</form>
</div>
</body>
</html>
<?php
//unset $_SESSION['idpers'];
mysql_free_result($rs_mod);
?>